package cn.lysj.framework.api.controller;

import cn.lysj.framework.api.base.ApiContent;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.coupon.domain.Coupon;
import cn.lysj.framework.coupon.util.JSSDKUtil;
import cn.lysj.framework.customer.domain.Customer;
import cn.lysj.framework.customer.domain.CustomerInfo;
import cn.lysj.framework.customer.domain.CustomerVip;
import cn.lysj.framework.customer.vo.CustomerVipCouponVo;
import cn.lysj.framework.customer.vo.CustomerVipInfoVo;
import cn.lysj.framework.store.domain.Store;
import cn.lysj.framework.store.service.StoreService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.method.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author ZhangWenJian
 * @data 2019/1/16--13:35
 * @description 顾客api控制类
 */
@RestController
@RequestMapping("/api/v1/customer")
@Api(value = "ApiCustomerController", tags = "顾客模块接口")
public class ApiCustomerController extends ApiContent {
    @Value("${event.title}")
    public String eventTitle;
    @Value("${event.info}")
    public String eventInfo;
    @Value("${event.img}")
    public String eventImg;

    //查询用户是否为会员
    @GetMapping("/get_customer_vip_info")
    public Resp getCustomerVipInfo(String storeId,String customerId){
        if(ParamUtil.isBlank(storeId) || ParamUtil.isBlank(customerId)){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        Store store = storeRepository.findOne(storeId);
        if(store == null){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        Customer customer = customerService.getRepository().findOne(customerId);
        if(customer == null){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        return customerVipService.judgeCustomerVip(storeId,customerId);
    }

    //获取用户vip数据 以及可以领取的红包
    @GetMapping("/get_customer_vip_date")
    public Resp getCustomerVipDate(String storeId,String customerId){
        return customerVipService.getCustomerVipDate(storeId,customerId);
    }
    //获取优惠券详情
    @GetMapping("/get_coupon_info")
    public Resp getCouponInfo(String couponId){
        Coupon coupon  = couponService.getRepository().findByIdAndDelFlag(couponId,1);
        Store store = storeRepository.findOne(coupon.getStoreId());
        if(ParamUtil.isBlank(coupon.getEventTitle())){
            if(!ParamUtil.isBlank(store.getEventTitle())){
                coupon.setEventTitle(store.getEventTitle());
            }
            else{
                coupon.setEventTitle(eventTitle);
            }
        }
        if(ParamUtil.isBlank(coupon.getEventInfo())){
            if(!ParamUtil.isBlank(store.getEventInfo())){
                coupon.setEventInfo(store.getEventInfo());
            }
            else{
                coupon.setEventInfo(eventInfo);
            }
        }
        if(ParamUtil.isBlank(coupon.getEventImg())){
            if(!ParamUtil.isBlank(store.getEventImg())){
                coupon.setEventImg(store.getEventImg());
            }
            else{
                coupon.setEventImg(eventImg);
            }
        }
        if(coupon == null){
            return new Resp(Resp.PARAM_ERROR,"优惠券已经失效");
        }
        if(coupon.getDeadlineType() == 1){
            if(new Date().compareTo(coupon.getUseEndDate()) != -1 ){
                return new Resp(Resp.PARAM_ERROR,"优惠券已经过期啦!");
            }
        }
        return new Resp(coupon);
    }

    //查询用户拥有的所有优惠券
    @GetMapping("/get_customer_vip_all_coupon")
    public Resp getCustomerVipAllCoupon(String storeId,String customerId){
        CustomerVipInfoVo customerVipInfoVo = customerVipService.getRepository().judgeCustomerVip(storeId,customerId);
        Map<String,Object> resultMap = new HashMap<>();

        if(customerVipInfoVo != null){
            resultMap.put("customerVipCouponInValid",customerVipCouponService.findCustomerVipCouponInValid(customerVipInfoVo.getCustomerVipId()).getObj());
            resultMap.put("customerVipAllCouponInUsed",customerVipCouponService.findCustomerVipAllCouponInUsed(customerVipInfoVo.getCustomerVipId()).getObj());
            resultMap.put("customerVipAllCouponInPastDue",customerVipCouponService.findCustomerVipAllCouponInPastDue(customerVipInfoVo.getCustomerVipId()).getObj());
            return new Resp(resultMap,"获取所有优惠券成功!");
        }
        else{
            List<CustomerVipCouponVo> customerVipCoupons = new ArrayList<>();
            resultMap.put("customerVipCouponInValid",customerVipCoupons);
            resultMap.put("customerVipAllCouponInUsed",customerVipCoupons);
            resultMap.put("customerVipAllCouponInPastDue",customerVipCoupons);
            return new Resp(resultMap,"该用户不是会员,没有优惠券信息!");
        }
    }

    //查询用户该金额下可使用的优惠券
    @GetMapping("/get_customer_vip_coupon_for_use")
    public Resp getCustomerVipCouponForUse(String storeId, String customerId, BigDecimal useMoney){
        CustomerVipInfoVo customerVipInfoVo = customerVipService.getRepository().judgeCustomerVip(storeId,customerId);
        if(customerVipInfoVo != null){
            return customerVipCouponService.getCustomerVipCouponForUse(customerVipInfoVo.getCustomerVipId(),useMoney);
        }
        else{
            List<CustomerVipCouponVo> customerVipCoupons = new ArrayList<>();
            return new Resp(customerVipCoupons,"该用户不是会员,没有优惠券信息!");
        }
    }

    //用户领取优惠券
    @PostMapping("/customer_get_coupon")
    public Resp customerGetCoupon(String customerId,String couponId){
        Customer customer = customerService.getRepository().findOne(customerId);
        if(customer == null){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        Coupon coupon = couponService.getRepository().findByIdAndDelFlag(couponId,1);
        if(coupon == null){
            return new Resp(Resp.PARAM_ERROR,"优惠券已经失效");
        }
        CustomerVipInfoVo customerVipInfoVo = customerVipService.getRepository().judgeCustomerVip(coupon.getStoreId(),customerId);
        if(customerVipInfoVo == null){
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("customerVipType",1);
            Integer customerInfoType = 1;
            if(!ParamUtil.isBlank(customer.getCustomerInfoId())){
                if(customerInfoService.getRepository().findOne(customer.getCustomerInfoId()) != null){
                    customerInfoType = 2;
                }
            }
            resultMap.put("customerInfoType",customerInfoType);
            return new Resp(resultMap,"系统错误,传输数据丢失!");
        }
        if(ParamUtil.isBlank(couponId)){
            return new Resp(Resp.PARAM_ERROR,"参数错误!");
        }
        //领取优惠券
        return couponService.customerVipGetCoupon(customerVipInfoVo.getCustomerVipId(),couponId,true);
    }

    //生日券领取接口
    @PostMapping("/customer_receive_birthday_coupon")
    public Resp customerReceiveBirthdayCoupon(String storeId,String customerId,Integer type){
        CustomerVipInfoVo customerVipInfoVo = customerVipService.getRepository().judgeCustomerVip(storeId,customerId);
        if(customerVipInfoVo == null){
            return new Resp(Resp.PARAM_ERROR,"非会员,领取失败!");
        }
        return birthdayCouponConfigService.receiveBirthdayCoupon(customerVipInfoVo.getCustomerVipId(),type);
    }

    //会员券领取接口
    @PostMapping("/customer_receive_new_vip_coupon")
    public Resp customerReceiveNewVipCoupon(String storeId,String customerId,Integer type){
        CustomerVipInfoVo customerVipInfoVo = customerVipService.getRepository().judgeCustomerVip(storeId,customerId);
        if(customerVipInfoVo == null){
            return new Resp(Resp.PARAM_ERROR,"非会员,领取失败!");
        }
        return newVipCouponConfigService.receiveNewVipCoupon(customerVipInfoVo.getCustomerVipId(),type);
    }

    //绑定已经拥有信息
    @PostMapping("/binding_customer_info")
    public Resp bingingCustomerInfo(String phone,String verifyCode,String customerInfoId,String customerId,String storeId){
        if(ParamUtil.isBlank(customerInfoId) || ParamUtil.isBlank(customerId) || ParamUtil.isBlank(storeId)){
            return new Resp(Resp.PARAM_ERROR,"传输数据异常");
        }
        if(ParamUtil.isBlank(phone)){
            return new Resp(Resp.PARAM_ERROR,"请输入手机号");
        }
        if(ParamUtil.isBlank(verifyCode)){
            return new Resp(Resp.PARAM_ERROR,"请输入验证码");
        }
        //验证手机号以及验证码
        Resp resp = verifyPhoneService.phoneVerify(phone,verifyCode);
        if (resp.getCode() != 200){
            return resp;
        }
        //建立与数据库中已经存在的customerInfo的联系
        resp = customerService.bingingCustomerInfo(customerInfoId,customerId);
        if (resp.getCode() != 200){
            return resp;
        }
        return resp;
    }


    //绑定用户信息 升级会员
    @PostMapping("/new_customer_info")
    public Resp buildCustomerVip(String customerId, String name, String phone,String verifyCode,Date birthday,String storeId){
        if(ParamUtil.isBlank(customerId) || ParamUtil.isBlank(storeId)){
            return new Resp(Resp.PARAM_ERROR,"传输数据异常");
        }
        if(ParamUtil.isBlank(name)){
            return new Resp(Resp.PARAM_ERROR,"操作失败,请输入姓名!");
        }
        if(ParamUtil.isBlank(birthday)){
            return new Resp(Resp.PARAM_ERROR,"操作失败,请输入生日!");
        }
        if(ParamUtil.isBlank(phone)){
            return new Resp(Resp.PARAM_ERROR,"请输入手机号");
        }
        if(ParamUtil.isBlank(verifyCode)){
            return new Resp(Resp.PARAM_ERROR,"请输入验证码");
        }
        return customerService.newCustomerInfo(storeId,customerId,name,phone,verifyCode,birthday);
    }

    //发送手机验证码
    @PostMapping("/send_phone_verify_code")
    public Resp sendPhoneVerifyCode(String phone){
        return verifyPhoneService.sendVerifyCode(phone,"86");
    }


    //分享卡券URL
    @PostMapping("/share_card_url")
    public  Resp shareCardUrl(String url){
        if (ParamUtil.isBlank(url)) {
            return new Resp(Resp.PARAM_ERROR, "系统错误,请重试");
        }
        return new Resp(JSSDKUtil.getJS_SDK_Map(url));
    }

    //成为会员
    @PostMapping("/become_store_vip")
    public Resp becomeStoreVip(String storeId,String customerId){
        if(ParamUtil.isBlank(storeId) || ParamUtil.isBlank(customerId)){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        Store store = storeRepository.findOne(storeId);
        if(store == null){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        Customer customer = customerService.getRepository().findOne(customerId);
        if(customer == null){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        if(ParamUtil.isBlank(customer.getCustomerInfoId())){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        CustomerInfo customerInfo = customerInfoService.getRepository().findOne(customer.getCustomerInfoId());
        if(customerInfo == null){
            return new Resp(Resp.PARAM_ERROR,"数据有误!");
        }
        customerVipService.buildCustomerVip(storeId,customerInfo.getId());
        return new Resp("加入会员成功!");
    }



}
